package com.library_manager_system.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import com.library_manager_system.Book;

public class LibraryDao {


	private static String string;

	public static void writeBook(Book book,int select) {
		// 将创建的人对象输入到AddressListDao.txt中；
		BufferedWriter savePerson;
		try {
			if(select == 1){
				savePerson = new BufferedWriter(new FileWriter("LibraryDao.txt",true));
				
			}else{
				savePerson = new BufferedWriter(new FileWriter("Statics_borrow_book.txt",true));
			}
			// 写入$@以区别人
			savePerson.write("$@");
			savePerson.newLine();

			String str = new String("书名==" + book.getBook_name());
			savePerson.write(str);
			savePerson.newLine();

			String str1 = new String("主编==" + book.getAuthor());
			savePerson.write(str1);
			savePerson.newLine();

			String str2 = new String("出版社==" + book.getPress());
			savePerson.write(str2);
			savePerson.newLine();

			String str3 = new String("出版年==" + book.getPress_year());
			savePerson.write(str3);
			savePerson.newLine();

			String str4 = new String("版次==" + book.getPress_time());
			savePerson.write(str4);
			savePerson.newLine();

			String str5 = new String("ISBN==" + book.getISBN());
			savePerson.write(str5);
			savePerson.newLine();
			
			String str6 = new String("定价==" + book.getBook_price());
			savePerson.write(str6);
			savePerson.newLine();
			
			String str7 = new String("拥有本数==" + book.getBook_number());
			savePerson.write(str7);
			savePerson.newLine();
			
			String str8 = new String("借出本数==" + book.getBorrow_book_number());
			savePerson.write(str8);
			savePerson.newLine();
			
			String str9 = new String("现余本数==" + book.getThe_rest_number());
			savePerson.write(str9);
			savePerson.newLine();
			
			// 关闭流
			savePerson.close();

		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}

	public static ArrayList<Book> readBook(int select) {
		ArrayList<Book> list = new ArrayList<Book>();
		BufferedReader read;
		try {
			if(select == 1){
				read = new BufferedReader(new FileReader("LibraryDao.txt"));
				
			}else{
				read = new BufferedReader(new FileReader("Statics_borrow_book.txt"));

			}
			String line = null;
			while((line = read.readLine())!=null){
				if (line.equals("$@")) {
					//书名
					StringBuffer str1 = new StringBuffer(read.readLine());
					int temp1 = str1.indexOf("=");
					str1.delete(0, temp1 + 2);
					String str_1 = str1.toString();
					//主编
					StringBuffer str2 = new StringBuffer(read.readLine());
					int temp2 = str2.indexOf("=");
					str2.delete(0, temp2 + 2);
					String str_2 = str2.toString();
					//出版社
					StringBuffer str3 = new StringBuffer(read.readLine());
					int temp3 = str3.indexOf("=");
					str3.delete(0, temp3 + 2);
					String str_3 = str3.toString();
					//出版年
					StringBuffer str4 = new StringBuffer(read.readLine());
					int temp4 = str4.indexOf("=");
					str4.delete(0, temp4 + 2);
					String str_4= str4.toString();
					//版次
					StringBuffer str5 = new StringBuffer(read.readLine());
					int temp5 = str5.indexOf("=");
					str5.delete(0, temp5 + 2);
					String str_5 = str5.toString();
					//ISBN
					StringBuffer str6 = new StringBuffer(read.readLine());
					int temp6 = str6.indexOf("=");
					str6.delete(0, temp6 + 2);
					String str_6 = str6.toString();
					
					//定价
					StringBuffer str7 = new StringBuffer(read.readLine());
					int temp7 = str7.indexOf("=");
					str7.delete(0, temp7 + 2);
					String str_7 = str7.toString();
					
					//用有本数
					StringBuffer str8 = new StringBuffer(read.readLine());
					int temp8 = str8.indexOf("=");
					str8.delete(0, temp8 + 2);
					String str_8 = str8.toString();
					//借出本数
					StringBuffer str9 = new StringBuffer(read.readLine());
					int temp9 = str9.indexOf("=");
					str9.delete(0, temp9 + 2);
					String str_9 = str9.toString();
					//剩余本数
					StringBuffer str10 = new StringBuffer(read.readLine());
					int temp10 = str10.indexOf("=");
					str10.delete(0, temp10 + 2);
					String str_10 = str10.toString();
					
					
					Book book = new Book(str_2, str_3,str_1, str_4,
							Integer.parseInt(str_5), 
							Integer.parseInt(str_6), 
							Integer.parseInt(str_7),
							Integer.parseInt(str_8),
							Integer.parseInt(str_9), 
							Integer.parseInt(str_10));
					list.add(book);
				}
			}
			read.close();		
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return list;
	}
	public static void updateLibraryDao(ArrayList<Book> list,int select){
		//对修改，删除过的list重新载入AddressListDao.txt文件中
		if(select == 1){
			File file = new File("LibraryDao.txt");
			if (file.exists()) {
				file.delete();
			}
			try {
				file.createNewFile();
				Iterator<Book> it1 = list.iterator();
				while (it1.hasNext()) {
					LibraryDao.writeBook(it1.next(),1);
				}
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}else{
			File file = new File("Statics_borrow_book.txt");
			if (file.exists()) {
				file.delete();
			}
			try {
				file.createNewFile();
				Iterator<Book> it1 = list.iterator();
				while (it1.hasNext()) {
					LibraryDao.writeBook(it1.next(),2);
				}
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
	}

}

